YOLOv3 in using cv2.dnn.readNetFrom()

In this lesson we'll learn how to load a pre-trained YOLOV3 Model and use OpenCV to run inferences over a few images

YOLO Object Detection

Steps Invovled

  1. Use Pretrained YOLOV3 weights (237MB)- https://pjreddie.com/media/files/yolov3.weights
  2. Create our blob object which is our loaded model
  3. Set the backend that runs the model

The input to the network is a called blob object.

The function cv.dnn.blobFromImage(img, scale, size, mean) transforms the image into a blob:

blob = cv.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=False)

It has the following parameters:

  1. the image to transform
  2. the scale factor (1/255 to scale the pixel values to [0..1])
  3. the size, here a 416x416 square image
  4. the mean value (default=0)
  5. the option swapBR=True (since OpenCV uses BGR)

Note A blob is a 4D numpy array object (images, channels, width, height). The image below shows the red channel of the blob. You notice the brightness of the red jacket in the background.

NOTE: How to Perform non maximum suppression given boxes and corresponding scores.

indices = cv.dnn.NMSBoxes( bboxes, scores, score_threshold, nms_threshold[, eta[, top_k]]

Parameters